package com.icore.ui;

import org.apache.http.message.BasicNameValuePair;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.icore.AppContext;
import com.icore.AppException;
import com.icore.R;
import com.icore.bean.User;
import com.icore.common.StringUtils;
import com.icore.common.UIHelper;
import com.icore.ui.base.BaseActivity;

/**
 * 登录页面
 * 
 */
@SuppressLint("HandlerLeak")
public class LoginActivity extends BaseActivity {

	private AutoCompleteTextView mAccount;
	private EditText mPwd;
	private CheckBox chb_rememberMe;
	private Button btn_login;

	private InputMethodManager imm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_view);

		// 网络连接判断
		if (!AppContext.getInstance().isNetworkConnected())
			UIHelper.ToastMessage(this, R.string.network_not_connected);

		imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

		mAccount = (AutoCompleteTextView) findViewById(R.id.login_account);
		mPwd = (EditText) findViewById(R.id.login_password);
		chb_rememberMe = (CheckBox) findViewById(R.id.login_checkbox_rememberMe);

		btn_login = (Button) findViewById(R.id.login_btn_login);
		btn_login.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 隐藏软键盘
				imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

				String account = mAccount.getText().toString();
				String pwd = mPwd.getText().toString();
				boolean isRememberMe = chb_rememberMe.isChecked();

				// 判断输入
				if (StringUtils.isEmpty(account)) {
					UIHelper.ToastMessage(v.getContext(), getString(R.string.msg_login_email_null));
					return;
				}
				if (StringUtils.isEmpty(pwd)) {
					UIHelper.ToastMessage(v.getContext(), getString(R.string.msg_login_pwd_null));
					return;
				}

				login(account, pwd, isRememberMe);
			}
		});
	}

	private ProgressDialog mProgress;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (mProgress != null) {
				mProgress.dismiss();
			}
			if (msg.what == 1) {
				User user = (User) msg.obj;
				int code = user.getResCode();
				// 失败，弹出提示框
				if (code == 0) {
					UIHelper.ToastMessage(LoginActivity.this, user.getResMessage());
				}
				// 成功，跳转
				else if (code == 1) {
					UIHelper.startActivity(LoginActivity.this, MainActivity.class,
							new BasicNameValuePair("rold_id", String.valueOf(user.getRoleID())),
							new BasicNameValuePair("user_id", String.valueOf(user.getUserID())));
				}
			} else {
				((AppException) msg.obj).makeToast(LoginActivity.this);
			}
		}
	};

	// 登录验证
	private void login(final String username, final String pwd, final boolean isRememberMe) {
		mProgress = ProgressDialog.show(LoginActivity.this, null, "加载中...", true, true);

		new Thread() {

			@Override
			public void run() {
				Message msg = new Message();
				try {
					User user = AppContext.server.login(username, pwd);
					msg.what = 1;
					msg.obj = user;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 监听返回--是否退出程序
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 是否退出应用
			UIHelper.Exit(this);
		} else {
			flag = super.onKeyDown(keyCode, event);
		}
		return flag;
	}
}
